<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
    .box{
                width: 400px;
                height: 400px;
                background: red;
                font-size: 30px;
                text-align: center;
                line-height: 400px;

    }
    </style>
</head>
<body>
    <input type="text" class="myinput" >
     <div class="box"></div>
    <script>
        let div= document.getElementsByClassName("box")[0]
        let i=0
        div.innerHTML =i
        //input事件
        function trigger(e) {
            console.log(e.target.value);
            }
        //一移入事件
        function move(e) {
            div.innerHTML = i++
            }    
        //防抖函数
        function fangdou(fun,time){
            let timer = null;
            return function(){
                clearTimeout(timer)
                timer=setTimeout(()=>{
                    fun.apply(this,arguments)
                },time)
            }
        }
        function out(){
            i = 0
        }
        //节流
        function throttle(fn, delay) {
        let valid = true;
        // return function () {
        //   /* 一开始就触发---后续每隔delay时间内只触发一次 */
        //   if (valid) {
        //     valid = false; // 关闭阀门
        //     // 如果阀门已经打开，就继续往下
        //     setTimeout(() => {
        //       fn.apply(this, arguments); // 定时器结束后执行
        //       valid = true; // 执行完成后打开阀门
        //     }, delay);
        //   }
        // };
        let previous = 0;
        return function() {
        let now = new Date();
        if (now - previous > delay) {
            fn.apply(this, arguments);
            previous = now;
        }
    };

      }
        let input= document.getElementsByClassName("myinput")[0]

        // input.oninput = fangdou(trigger,1000)
        input.oninput = throttle(trigger,500)
        // div.addEventListener('mousemove',move)
        div.onmouseout=out
        div.onmousemove=move
    </script>
</body>
</html>